package com.travel.server.manager.impl.command.order.data;

import com.travel.common.CommonConstant;
import com.travel.common.excel.ExcelData;
import com.travel.common.exception.WorkCode;
import com.travel.common.exception.WorkException;
import com.travel.common.util.DateUtil;
import com.travel.common.util.ExcelUtil;
import com.travel.server.manager.dto.request.order.data.ExportOrderConstituteRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.dto.response.order.data.QueryOrderConstituteResponse.OrderConstituteItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.sal.OrderDataSal;
import com.travel.server.manager.impl.vo.DataDateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/7 16:30
 * @Desc:
 */
@Component
@Slf4j
public class ExportOrderConstituteCommand extends AbstractCommand<ExportOrderConstituteRequest, EmptyResponse> {

    @Autowired
    private QueryOrderConstituteCommand queryOrderConstituteCommand;

    @Override
    protected void checkInput(ExportOrderConstituteRequest request) {
        checkTimeInput(request);
        if(request.getType() == null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(ExportOrderConstituteRequest request) {
        checkInput(request);
        Integer type  = request.getType();
        String compare = OrderDataSal.getCompare(request.getType());

        DataDateVo dateVo = OrderDataSal.getDate(type, request);
        Date lastTime = dateVo.getLastTime();
        Date endTime = dateVo.getEndTime();
        List<OrderConstituteItem> list = queryOrderConstituteCommand.makeConstitute(lastTime, endTime);

        List<String> title = Arrays.asList("用户类型", "付款金额", compare, "付款订单数", compare, "付款人数", compare);
        List<List<Object>> rows = new ArrayList<>();
        for(OrderConstituteItem item : list){
            List<Object> objects = new ArrayList<>();
            objects.add(item.getUserType().equals(CommonConstant.YES)?"会员":"非会员");
            objects.add(item.getPayAmount());
            objects.add(item.getPayAmountCompare());
            objects.add(item.getPayOrder());
            objects.add(item.getPayOrderCompare());
            objects.add(item.getPayNum());
            objects.add(item.getPayNumCompare());
            rows.add(objects);
        }
        ExcelData excelData = new ExcelData();
        excelData.setTitles(title);
        excelData.setRows(rows);
        excelData.setName("交易构成");
        ExcelUtil.exportExcel(getHttpServletResponse(), "交易构成", excelData);

        return new EmptyResponse();
    }
}
